<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>类方法和实例方法 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter02.html" />
    
    
    <link rel="prev" href="./index.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="1"
        data-chapter-title="类方法和实例方法"
        data-filepath="chapter01.md"
        data-basepath="."
        data-revision="Sun Feb 23 2020 13:59:40 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        类方法和实例方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        单例设计模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        继承的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        继承的注意使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象相关的运算符和内置函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        多态的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x7C7B;&#x65B9;&#x6CD5;&#x3001;&#x9759;&#x6001;&#x65B9;&#x6CD5;">&#x7C7B;&#x65B9;&#x6CD5;&#x3001;&#x9759;&#x6001;&#x65B9;&#x6CD5;</h2>
<h3 id="1-&#x7C7B;&#x65B9;&#x6CD5;">1. &#x7C7B;&#x65B9;&#x6CD5;</h3>
<ul>
<li><strong>&#x7B2C;&#x4E00;&#x4E2A;&#x5F62;&#x53C2;&#x662F;&#x7C7B;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;</strong></li>
<li>&#x9700;&#x8981;&#x7528;&#x88C5;&#x9970;&#x5668;<code>@classmethod</code>&#x6765;&#x6807;&#x8BC6;&#x5176;&#x4E3A;&#x7C7B;&#x65B9;&#x6CD5;&#xFF0C;&#x5BF9;&#x4E8E;&#x7C7B;&#x65B9;&#x6CD5;&#xFF0C;<strong>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x662F;&#x7C7B;&#x5BF9;&#x8C61;</strong>&#xFF0C;&#x4E00;&#x822C;&#x4EE5;<code>cls</code>&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span><span class="hljs-params">(object)</span>:</span>
    __type = <span class="hljs-string">&quot;&#x72D7;&quot;</span>

    <span class="hljs-comment"># &#x7C7B;&#x65B9;&#x6CD5;&#xFF0C;&#x7528;classmethod&#x6765;&#x8FDB;&#x884C;&#x4FEE;&#x9970;</span>
<span class="hljs-decorator">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_type</span><span class="hljs-params">(cls)</span>:</span>
        <span class="hljs-keyword">return</span> cls.__type
print(Dog.get_type())
</code></pre>
<blockquote>
<p>&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF1A;</p>
<ul>
<li>&#x5F53;&#x65B9;&#x6CD5;&#x4E2D; <strong>&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7C7B;&#x5BF9;&#x8C61;</strong> (&#x5982;&#x8BBF;&#x95EE;&#x79C1;&#x6709;&#x7C7B;&#x5C5E;&#x6027;&#x7B49;)&#x65F6;&#xFF0C;&#x5B9A;&#x4E49;&#x7C7B;&#x65B9;&#x6CD5;</li>
<li>&#x7C7B;&#x65B9;&#x6CD5;&#x4E00;&#x822C;&#x548C;&#x7C7B;&#x5C5E;&#x6027;&#x914D;&#x5408;&#x4F7F;&#x7528;</li>
</ul>
</blockquote>
<h3 id="2-&#x9759;&#x6001;&#x65B9;&#x6CD5;">2. &#x9759;&#x6001;&#x65B9;&#x6CD5;</h3>
<ul>
<li>&#x9700;&#x8981;&#x901A;&#x8FC7;&#x88C5;&#x9970;&#x5668;<code>@staticmethod</code>&#x6765;&#x8FDB;&#x884C;&#x4FEE;&#x9970;&#xFF0C;<strong>&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x65E2;&#x4E0D;&#x9700;&#x8981;&#x4F20;&#x9012;&#x7C7B;&#x5BF9;&#x8C61;&#x4E5F;&#x4E0D;&#x9700;&#x8981;&#x4F20;&#x9012;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#xFF08;&#x5F62;&#x53C2;&#x6CA1;&#x6709;self/cls&#xFF09;</strong>&#x3002;</li>
<li>&#x9759;&#x6001;&#x65B9;&#x6CD5; &#x4E5F;&#x80FD;&#x591F;&#x901A;&#x8FC7; <strong>&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</strong> &#x548C; <strong>&#x7C7B;&#x5BF9;&#x8C61;</strong> &#x53BB;&#x8BBF;&#x95EE;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span><span class="hljs-params">(object)</span>:</span>
    type = <span class="hljs-string">&quot;&#x72D7;&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        name = <span class="hljs-keyword">None</span>

    <span class="hljs-comment"># &#x9759;&#x6001;&#x65B9;&#x6CD5;    </span>
<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">introduce</span><span class="hljs-params">()</span>:</span>  <span class="hljs-comment"># &#x9759;&#x6001;&#x65B9;&#x6CD5;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x4F20;&#x9012;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x548C;&#x7C7B;&#x5BF9;&#x8C61;</span>
        print(<span class="hljs-string">&quot;&#x72AC;&#x79D1;&#x54FA;&#x4E73;&#x52A8;&#x7269;,&#x5C5E;&#x4E8E;&#x98DF;&#x8089;&#x76EE;..&quot;</span>)

dog1 = Dog()
Dog.introduce()    <span class="hljs-comment"># &#x53EF;&#x4EE5;&#x7528; &#x5B9E;&#x4F8B;&#x5BF9;&#x8C61; &#x6765;&#x8C03;&#x7528; &#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
dog1.introduce()    <span class="hljs-comment"># &#x53EF;&#x4EE5;&#x7528; &#x7C7B;&#x5BF9;&#x8C61; &#x6765;&#x8C03;&#x7528; &#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
</code></pre>
<blockquote>
<p>&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF1A;</p>
<ul>
<li>&#x5F53;&#x65B9;&#x6CD5;&#x4E2D; <strong>&#x65E2;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</strong>(&#x5982;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#xFF0C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;)&#xFF0C;<strong>&#x4E5F;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7C7B;&#x5BF9;&#x8C61;</strong> (&#x5982;&#x7C7B;&#x5C5E;&#x6027;&#x3001;&#x7C7B;&#x65B9;&#x6CD5;&#x3001;&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x7B49;)&#x65F6;&#xFF0C;&#x5B9A;&#x4E49;&#x9759;&#x6001;&#x65B9;&#x6CD5;</li>
<li><strong>&#x53D6;&#x6D88;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;&#x4F20;&#x9012;</strong>&#xFF0C;&#x6709;&#x5229;&#x4E8E; <strong>&#x51CF;&#x5C11;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5185;&#x5B58;&#x5360;&#x7528;&#x548C;&#x6027;&#x80FD;&#x6D88;&#x8017;</strong></li>
</ul>
</blockquote>
<h3 id="&#x6CE8;&#x610F;&#x70B9;&#xFF1A;">&#x6CE8;&#x610F;&#x70B9;&#xFF1A;</h3>
<ul>
<li>&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x540C;&#x540D;&#x7684;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x4F1A;&#x6267;&#x884C;&#x6700;&#x540E;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_method</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&quot;</span>)

<span class="hljs-decorator">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_method</span><span class="hljs-params">(cls)</span>:</span>
        print(<span class="hljs-string">&quot;&#x7C7B;&#x65B9;&#x6CD5;&quot;</span>)

<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_method</span><span class="hljs-params">()</span>:</span>  <span class="hljs-comment"># &#x88AB;&#x6700;&#x540E;&#x5B9A;&#x4E49;</span>
        print(<span class="hljs-string">&quot;&#x9759;&#x6001;&#x65B9;&#x6CD5;&quot;</span>)

dog1 = Dog()
Dog.demo_method()  <span class="hljs-comment"># &#x7ED3;&#x679C;: &#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
dog1.demo_method()  <span class="hljs-comment"># &#x7ED3;&#x679C;: &#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./index.html" class="navigation navigation-prev " aria-label="Previous page: 学习目标"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter02.html" class="navigation navigation-next " aria-label="Next page: 单例设计模式"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
